package com.tang.ack.provider;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class RabbitMqLogInfoProduce {

    private final AmqpTemplate rabbitmqAmqpTemplate;

    // 交换器的名称Exchange
    private final String exchange;

    // 路由键routingKey
    private final String routingKey;

    public RabbitMqLogInfoProduce(
            AmqpTemplate rabbitmqAmqpTemplate,
            @Value(value = "${rabbitmq.config.exchange}") String exchange,
            @Value(value = "${rabbitmq.config.queue.info.routing.key}") String routingKey) {
        this.rabbitmqAmqpTemplate = rabbitmqAmqpTemplate;
        this.exchange = exchange;
        this.routingKey = routingKey;
    }

    /**
     * 发送消息的方法
     *
     * @param msg 发送消息
     */
    public void producer(String msg) {
        // 向消息队列发送消息
        // 参数1,交换器的名称
        // 参数2,路由键
        // 参数3,消息
        this.rabbitmqAmqpTemplate.convertAndSend(this.exchange, this.routingKey, msg);
    }

}
